﻿@using MvcContrib.UI.Grid
@using XRM.Security.DataAccess.Enums
@model XRM.Security.WebUI.Models.Request.RequestSearchModel
@{
    ViewBag.Title = "Заявки";
    ViewBag.HideHeader = true;
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@section styles
{
    @Styles.Render("~/bundles/jquery-css")
    
    <style type="text/css">
        .filled a {
            display: inline-block; 
            height: 100%; 
            width: 100%;
        }
        .filled span {
            display: block;
            margin: 8px;
        }
        .table tbody tr td.filled {
            padding: 0;
        }
    </style>
}

<div>
    @using (Html.BeginForm("Filter", "Request", FormMethod.Post, new { @class = "form-horizontal form-horizontal-2", id = "filterForm" }))
    {
        <div class="form-group">
            @Html.LabelFor(m => m.TextFilter, new { @class = "control-label" })
            <div class="control-group">
                    <div class="col-sm-5">
                        <div class="input-group">
                            @Html.TextBoxFor(m => m.TextFilter, new { @class = "form-control", title = "Введите ФИО пользователя или наименование подразделения" })
                            @Html.Partial("ClearButtonPartial")
                        </div>
                    </div>
                    <div class="col-sm-1">
                        <button class="btn btn-primary btn-block" type="submit" id="btnFind">Найти</button>
                    </div>
            </div>
        </div>
        
        <div class="form-group">
                <label class = "control-label">&nbsp;</label>
                <div class="control-group">
                    <div class="col-sm-5">
                        <div class="checkbox">
                            <label for="ShowAllFilter">
                                @Html.CheckBoxFor(x => x.ShowAllFilter)
                                Искать, включая выполненные заявки
                            </label>
                        </div>    
                    </div>
                </div>
        </div>
    }
    <hr/>

    <p class="actions-centered">
        @Html.ActionLink("Создать заявку", "Edit", "Request", null, new { @class = "btn btn-large btn-primary" })
    </p>
</div>

    @{
        if (Model.RequestList != null && Model.RequestList.Any())
        {
            Html.Grid(Model.RequestList).Columns(column =>
            {
                column.Custom(@<text>
                                   <input class="hidden" type="text" value="@item.Number.ToString("00000000000000000000")" />
                                   <a href="@Url.Action("Edit", "Request", new { id = item.Id })">
                                       <span>
                                            <i class="fa @(item.StatusIcon)" title="@item.Status.Description()" rel="tooltip">&nbsp;</i>@item.Number
                                       </span>
                                   </a> 
                               </text>)
                    .Named( "Номер" ).Attributes( style => "min-width: 80px;", @class => "filled" );
                column.For(item => Html.ActionLink(item.UserLastName + " " + item.UserFirstName + " " + item.UserMiddleName, "Edit", "UserRegister", new { id = item.UserId }, null))
                    .Named( "Пользователь" ).Attributes( nowrap => "nowrap" );
                column.For(item => item.Department).Named("Подразделение");
                column.For(item => item.DepartmentAddress).Named("Адрес");
                column.Custom(@<text>
                                   @foreach (var work in item.WorkKinds)
                                   {
                                       <div>@work</div>
                                   }
                               </text>).Named("Вид работ");
                //column.For(item => item.Status.Description()).Named("Статус").Attributes(nowrap => "nowrap");
                column.For( item => item.Deadline.HasValue ? item.Deadline.Value.ToString( "dd.MM.yyyy" ) : String.Empty).Named( "Крайний срок" );
                column.For(item => item.CreateDate.ToString("dd.MM.yyyy")).Named("Дата создания");
                column.For(item => item.CloseDate.HasValue? item.CloseDate.Value.ToString("dd.MM.yyyy"):"").Named("Дата закрытия");

            }).Attributes(@class => "table table-striped table-bordered", id => "table_id")
              .Empty("Нет данных для отображения")
              .Render();
        } 
        else if (Model.RequestList != null)
        {
            <strong>Нет заявок, удовлетворяющих критериям поиска.</strong>
        }
    }


@section scripts
{
    @Scripts.Render("~/bundles/gridlibrary")
    <script type="text/javascript" src="@(Url.Content("~/Scripts/AjaxRequest.js"))"></script>    

    <script type="text/javascript">
        $(document).ready(function () {
            @if (Model.RequestList != null && Model.RequestList.Any())
            {
                <text>
                $('#table_id').dataTable({
                    paging: false,
                    bFilter: false,
                    bSort: true,
                    info: false,
                    "order": [[0, "desc"]],
                    "columnDefs": [
                        { "type": "date-eu", targets: 5 }, { "type": "date-eu", targets: 6 }, { "type": "date-eu", targets: 7 },
                        { "orderDataType": "dom-text", targets: 0 }, { 'bSortable': false, 'aTargets': [4] }
                    ]
                });
                </text>
            }

            PrepareAutocompleteEdit("TextFilter", '@(Url.Action("FilterAutocomplete"))')
        });

    </script>
}